/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package input;

import java.util.ArrayList;
import java.util.Collection;

import tools.InputType;
import tools.StringTools;
import botobjects.Command;

/**
 * A class that represents a command
 * 
 * 
 */
public class IRCCommand extends ArrayList<String> {
   private static final long serialVersionUID = -6535984246758148942L;
   private Command command = null;
   private final int source;
   private final String sender;
   private final String channel;

   public IRCCommand(final int source, final String sender, final String channel) {
	  super();
	  this.source = source;
	  this.sender = sender;
	  this.channel = channel;
   }

   public IRCCommand(final int source, final String sender, final String channel, final Collection<String> asList) {
	  super(asList);
	  this.source = source;
	  this.sender = sender;
	  this.channel = channel;
   }

   public String getChannel() {
	  return channel;
   }

   /**
    * Returns a Command object for this command
    * 
    * @return
    */
   public Command getCommand() {
	  if (command == null) {
		 command = Command.getCommand(get(0));
	  }
	  return command;
   }

   /**
    * Returns the full message from IRC
    * 
    * @return
    */
   public String getFullMessage() {
	  return getPartialMessage(1, size());
   }

   /**
    * Returns part of the message from IRC
    * 
    * @param start
    * @param end
    * @return
    */
   public String getPartialMessage(final int start, final int end) {
	  String out = "";
	  for (int i = start; i < Math.min(size(), end); ++i) {
		 out += get(i) + " ";
	  }
	  return out.trim();
   }

   public String getSender() {
	  return sender;
   }

   public int getSource() {
	  return source;
   }

   /**
    * Checks if the specified position in the list is a double
    * 
    * @param pos
    * @return
    */
   public boolean isDouble(final int pos) {
	  return validate(pos, InputType.DOUBLE);
   }

   /**
    * Checks if the specified position in the list is an int
    * 
    * @param pos
    * @return
    */
   public boolean isInt(final int pos) {
	  return validate(pos, InputType.INT);
   }

   /**
    * Validates the string found at the specified position against the specified InputType
    * 
    * @param pos
    * @param it
    * @return
    */
   public boolean validate(final int pos, final InputType it) {
	  return StringTools.validateInput(it, get(pos));
   }

   /**
    * Validates the specified part of the command message against the supplied InputType
    * 
    * @param start
    * @param end
    * @param it
    * @return
    */
   public boolean validate(final int start, final int end, final InputType it) {
	  return StringTools.validateInput(it, getPartialMessage(start, end));
   }
}
